var user_address_set_ops={
	init:function(){
		this.eventBind();
	},
	eventBind:function(){
		//点击省份改变事件
		$("#province_id").change(function(){
			
			var id=$(this).val();	
		
			
			$.ajax({
				url:common_ops.buildMUrl('/user/address_set'),
				type:"POST",
				dataType:"json",
				data:{
					id:id
				},
				success:function(res){
					
					callback=null;
					if(res.code==200){
						var list=JSON.parse(res.data.list);
						$("#city_id").empty();
						
						for(var i=0;i<list.length;i++){
							$("#city_id").append('<option value="'+list[i].id+'">'+list[i].name+'</option>');
						}
						
					}
				},
			});
		});
		//点击市区改变事件
		$(".select_box").on('change','#city_id',function(){
			var city_id=$(this).val();	
			$.ajax({
				url:common_ops.buildMUrl('/user/address_set'),
				type:"POST",
				dataType:"json",
				data:{
					city_id:city_id
				},
				success:function(res){
					
					callback=null;
					$("#area_id").empty();
					if(res.code==200){
						var list=JSON.parse(res.data.list);
						
						for(var i=0;i<list.length;i++){
							$("#area_id").append('<option value="'+list[i].id+'">'+list[i].name+'</option>');
						}
						
					}
				},
			});
		});
		//点击保存事件
		$(".save").click(function(){
			var province_id=$("#province_id").val();//省id
			var city_id=$("#city_id").val();//市区
			var area_id=$("#area_id").val();//县

			var nickname=$("input[name=nickname]").val();
			var nickname_target=$("input[name=nickname]");
			
			var mobile=$("input[name=mobile]").val();
			var mobile_target=$("input[name=nickname]");
			var address=$(".addr_textarea").val();
			var address_target=$(".addr_textarea");
			var id=$("input[name=id]").val();
			if($.trim(nickname).length<1){
				common_ops.tip('姓名不能为空',nickname_target);
				return;
			}
			if($.trim(mobile).length<1){
				common_ops.tip('手机号不能为空',mobile_target);
				return;
				
			}
			if($.trim(address).length<1){
				common_ops.tip('详细地址不能为空',address_target);
				return;
				
			}
			$.ajax({
				url:common_ops.buildMUrl('/user/address_set'),
				type:'post',
				dataType:'json',
				data:{
					nickname:nickname,
					mobile:mobile,
					address:address,
					province_id:province_id,
					city_id:city_id,
					area_id:area_id,
					id:id,
					act:'add',
				},
				success:function(res){
					callback=null;
					if(res.code==200){
						callback=function(){
							window.location.href=window.location.href;
						}
					}
					common_ops.alert(res.msg,callback);
				},
			});
		});
		//点击删除
		$(".del").click(function(){
			var id=$(this).attr('data');
			$.ajax({
				url:common_ops.buildMUrl('/user/address_set'),
				type:'post',
				dataType:'json',
				data:{
					address_id:id,
					act:'del',
				},
				success:function(res){
					callback=null;
					if(res.code==200){
						callback=function(){
							window.location.href=window.location.href;
						}
					}
					common_ops.alert(res.msg,callback);
				},
			});
		})
		//设为默认
		$(".set_default").click(function(){
			var id=$(this).attr('data');
			$.ajax({
				url:common_ops.buildMUrl('/user/address_set'),
				type:'post',
				dataType:'json',
				data:{
					address_id:id,
					act:'set_default',
				},
				success:function(res){
					callback=null;
					if(res.code==200){
						callback=function(){
							window.location.href=window.location.href;
						}
					}
					common_ops.alert(res.msg,callback);
				},
			});
		})
	},
};
$(document).ready(function(){
	user_address_set_ops.init();
});